
#loader 1KB
#kernal image 100KB
LOADER_SECTOR:=2
KERNAL_SECTOR:=200
KERNAL_VADDR:=0xc0001500
boot:=boot/
include:=include/
lib:=lib/
kernal:=kernal/
device:=device/
thread:=thread/
user:=user/
shell:=shell/
out:=out/
process:=process/
fs:=fs/
ulib:=ulib/

kernal_include_files:=-I $(device) -I $(lib) -I $(lib)$(kernal) -I $(kernal) -I $(thread) -I $(process) -I $(lib)$(user) -I $(fs) -I $(shell) -I $(ulib)

user_include_files:=-I $(ulib)

KERNAL_CFLAGS:= -Wall $(kernal_include_files) -c -nostdinc -fno-builtin -W -Wstrict-prototypes \
-Wmissing-prototypes

USER_CFLAGS:= -Wall $(user_include_files) -c -nostdinc -fno-builtin -W -Wstrict-prototypes \
-Wmissing-prototypes

as:=nasm -i $(boot)$(include)
objdump:=objdump

kernal_objs:=$(out)print.o $(out)init.o $(out)interrupt.o $(out)tss.o \
      $(out)main.o $(out)intr.o $(out)timer.o $(out)kdebug.o \
      $(out)kstring.o $(out)bitmap.o $(out)circle_buffer.o $(out)memory.o $(out)thread.o \
      $(out)list.o $(out)switch.o $(out)sync.o $(out)console.o $(out)keyboard.o \
      $(out)process.o $(out)syscall_entry.o $(out)syscall.o $(out)syscall_init.o \
      $(out)stdio.o $(out)stdio_kernal.o $(out)ide.o $(out)fs.o $(out)inode.o \
      $(out)dir.o $(out)file.o $(out)fork.o $(out)yi_shell.o $(out)buildin_cmd.o \
      $(out)elf32.o $(out)exec.o $(out)exit.o $(out)wait.o


#we used Bochs x86 Emulator 2.7
simulator := bochs -f

#------------------------------KERNAL COMPLIE----------------------------------

$(out)hd64M.img : $(out)mbr.bin $(out)loader.bin $(out)kernal.bin $(out)hello
	echo yes | bximage -q -hd=64 -func=create -imgmode=flat $@ > /dev/null
	dd if=$(out)mbr.bin of=$@ bs=512 count=1 conv=notrunc
	dd if=$(out)loader.bin of=$@ bs=512 count=$(LOADER_SECTOR) seek=2 conv=notrunc
	dd if=$(out)kernal.bin of=$@ bs=512 count=$(KERNAL_SECTOR) seek=8 conv=notrunc
	dd if=$(out)hello of=$@ bs=512 count=10 seek=300 conv=notrunc

$(out)mbr.bin: $(boot)MBR.S $(boot)$(include)boot.inc
	$(as) $< -o $@ -l $(addsuffix .lst , $(basename $@)) -D LOADER_SECTOR=$(LOADER_SECTOR)

$(out)loader.bin: $(boot)loader.S $(boot)$(include)boot.inc
	$(as) $< -o $@ -l $(addsuffix .lst , $(basename $@)) -D KERNAL_SECTOR=$(KERNAL_SECTOR)

$(out)kernal.bin: $(kernal_objs)
	ld $^ -Ttext $(KERNAL_VADDR) -e main -Map $(out)kernal.map -o $@
	$(objdump) -s -d $@ > $(out)kernal.lst

$(out)main.o: $(kernal)main.c
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)print.o: $(lib)$(kernal)print.S
	$(as) $< -f elf -o $@ -l $(addsuffix .lst , $(basename $@))

$(out)switch.o: $(thread)switch.S
	$(as) $< -f elf -o $@ -l $(addsuffix .lst , $(basename $@))

$(out)syscall_entry.o: $(kernal)syscall_entry.S
	$(as) $< -f elf -o $@ -l $(addsuffix .lst , $(basename $@))

$(out)init.o: $(kernal)init.c
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)tss.o: $(kernal)tss.c $(kernal)tss.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)intr.o: $(kernal)intr.S
	$(as) $< -f elf -o $@ -l $(addsuffix .lst , $(basename $@))

$(out)interrupt.o: $(kernal)interrupt.c
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)timer.o: $(device)timer.c $(device)timer.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)console.o: $(device)console.c $(device)console.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)keyboard.o: $(device)keyboard.c $(device)keyboard.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)kdebug.o:$(kernal)debug.c $(kernal)debug.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)kstring.o:$(lib)string.c $(lib)string.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)bitmap.o:$(lib)$(kernal)bitmap.c $(lib)$(kernal)bitmap.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)circle_buffer.o:$(lib)$(kernal)circle_buffer.c $(lib)$(kernal)circle_buffer.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)memory.o:$(kernal)memory.c $(kernal)memory.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)thread.o:$(thread)thread.c $(thread)thread.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)list.o:$(lib)$(kernal)list.c $(lib)$(kernal)list.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)sync.o:$(thread)sync.c $(thread)sync.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)process.o:$(process)process.c $(process)process.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)syscall.o:$(ulib)syscall.c $(ulib)syscall.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)syscall_init.o:$(lib)$(user)syscall_init.c $(lib)$(user)syscall_init.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)fork.o:$(lib)$(user)fork.c $(lib)$(user)fork.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)exec.o:$(lib)$(user)exec.c $(lib)$(user)exec.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)exit.o:$(lib)$(user)exit.c $(lib)$(user)exit.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)wait.o:$(lib)$(user)wait.c $(lib)$(user)wait.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)elf32.o:$(lib)$(user)elf32.c $(lib)$(user)elf32.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)stdio.o:$(ulib)stdio.c $(ulib)stdio.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)stdio_kernal.o:$(lib)$(kernal)stdio_kernal.c $(lib)$(kernal)stdio_kernal.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)ide.o:$(device)ide.c $(device)ide.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)fs.o:$(fs)fs.c $(fs)fs.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)inode.o:$(fs)inode.c $(fs)inode.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)dir.o:$(fs)dir.c $(fs)dir.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)file.o:$(fs)file.c $(fs)file.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)yi_shell.o: $(shell)yi_shell.c $(shell)yi_shell.h
	gcc $< $(KERNAL_CFLAGS) -o $@

$(out)buildin_cmd.o: $(shell)buildin_cmd.c $(shell)buildin_cmd.h
	gcc $< $(KERNAL_CFLAGS) -o $@

#------------------------------SHELL CMD COMPLIE-------------------------------

crt_objs:=$(out)udebug.o $(out)ustring.o $(out)stdio.o  $(out)syscall.o $(out)start.o

$(out)start.o:$(ulib)start.S
	$(as) $< -f elf -o $@

$(out)ustring.o:$(ulib)string.c $(ulib)string.h
	gcc $< $(USER_CFLAGS) -o $@

$(out)udebug.o:$(ulib)debug.c $(ulib)debug.h
	gcc $< $(USER_CFLAGS) -o $@

$(out)crt.a: $(crt_objs)
	ar rcs $@ $^

$(out)hello.o:$(shell)hello.c
	gcc $< $(USER_CFLAGS) -o $@

$(out)hello:$(out)hello.o $(out)crt.a
	ld $^ -o $@
	$(objdump) -s -d $@ > $@.lst

run:
	@$(simulator) brc.txt

clean:
	@rm $(out)*
